package com.fr.base.xml;

import com.fr.base.core.util.Ascii85InputStream;
import com.fr.base.core.util.Ascii85OutputStream;
import com.fr.util.Utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

public class XMLEncodeUtils
{
  private static String[] deprecated_XmlImageDataArray = { "]]", "]]A" };
  private static String[] xmlImageDataArray = { "]", "]A" };

  public static byte[] zipDeflate(byte[] paramArrayOfByte)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      DeflaterOutputStream localDeflaterOutputStream = new DeflaterOutputStream(localByteArrayOutputStream, new Deflater(9, true), 512);
      localDeflaterOutputStream.write(paramArrayOfByte, 0, paramArrayOfByte.length);
      localDeflaterOutputStream.close();
    }
    catch (Exception localException)
    {
    }
    return localByteArrayOutputStream.toByteArray();
  }

  public static byte[] zipInflate(byte[] paramArrayOfByte)
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      InflaterInputStream localInflaterInputStream = new InflaterInputStream(localByteArrayInputStream, new Inflater(true), 512);
      byte[] arrayOfByte = new byte[512];
      while ((i = localInflaterInputStream.read(arrayOfByte, 0, arrayOfByte.length)) >= 0)
      {
        int i;
        localByteArrayOutputStream.write(arrayOfByte, 0, i);
      }
    }
    catch (IOException localIOException)
    {
    }
    return localByteArrayOutputStream.toByteArray();
  }

  public static byte[] encodeAscii85(byte[] paramArrayOfByte)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      Ascii85OutputStream localAscii85OutputStream = new Ascii85OutputStream(localByteArrayOutputStream);
      localAscii85OutputStream.write(paramArrayOfByte, 0, paramArrayOfByte.length);
      localAscii85OutputStream.close();
    }
    catch (Exception localException)
    {
    }
    return localByteArrayOutputStream.toByteArray();
  }

  public static byte[] decodeAscii85(byte[] paramArrayOfByte)
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      Ascii85InputStream localAscii85InputStream = new Ascii85InputStream(localByteArrayInputStream);
      byte[] arrayOfByte = new byte[512];
      while ((i = localAscii85InputStream.read(arrayOfByte, 0, arrayOfByte.length)) >= 0)
      {
        int i;
        localByteArrayOutputStream.write(arrayOfByte, 0, i);
      }
    }
    catch (IOException localIOException)
    {
    }
    return localByteArrayOutputStream.toByteArray();
  }

  public static String encodeBytesAsString(byte[] paramArrayOfByte)
  {
    try
    {
      return new String(encodeAscii85(zipDeflate(paramArrayOfByte)), "ISO-8859-1");
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    return "";
  }

  public static byte[] decodeStringAsBytes(String paramString)
  {
    try
    {
      return zipInflate(decodeAscii85(paramString.getBytes("ISO-8859-1")));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      return new byte[0];
    }
    catch (Exception localException)
    {
    }
    return new byte[0];
  }

  public static String wrapCDATA(String paramString)
  {
    return "<![CDATA[" + cdataEncode(paramString) + "]]>";
  }

  public static String cdataEncode(String paramString)
  {
    return Utils.replaceAllString(paramString, xmlImageDataArray[0], xmlImageDataArray[1]);
  }

  public static String cdataDecode(String paramString)
  {
    return Utils.replaceAllString(paramString, xmlImageDataArray[1], xmlImageDataArray[0]);
  }

  public static String deprecatedCDATADecode(String paramString)
  {
    return Utils.replaceAllString(paramString, deprecated_XmlImageDataArray[1], deprecated_XmlImageDataArray[0]);
  }
}